package cn.itcast.service.impl;

import cn.itcast.dao.DingDanDao;
import cn.itcast.domain.DingDan;
import cn.itcast.entity.PageResult;
import cn.itcast.service.DingDanService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * Created by leiylong on 2020/4/21
 */
@Service("dinDanService")
public class DingDanServiceImpl implements DingDanService {
    @Autowired
    private DingDanDao dingDanDao;

    @Override
    public List<Map> findDate(String date) {
        return dingDanDao.findDate(date);
    }

    @Override
    public List<DingDan> findAll() {
        return dingDanDao.findAll();
    }

    @Override
    public DingDan findById(String orderId) {
        return dingDanDao.findById(orderId);
    }

    @Override
    public PageResult<DingDan> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<DingDan> dingDanPage = (Page<DingDan>) dingDanDao.findPage();
        return new PageResult<DingDan>(dingDanPage.getTotal(), dingDanPage.getResult());

    }

    @Override
    public void add(DingDan dingDan) {
        dingDanDao.add(dingDan);
    }

    @Override
    public void update(DingDan dingDan) {
        dingDanDao.update(dingDan);
    }

    @Override
    public List<DingDan> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return dingDanDao.selectByExample(example);
    }


    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(DingDan.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("d_id") != null) {
                criteria.andEqualTo("d_id", searchMap.get("d_id"));
            }
            if (searchMap.get("d_name") != null && !"".equals(searchMap.get("d_name"))) {
                criteria.andLike("d_name", "%" + searchMap.get("d_name") + "%");
            }
            if (searchMap.get("d_count") != null) {
                criteria.andEqualTo("d_count", searchMap.get("d_count"));
            }
            if (searchMap.get("d_date") != null) {
                criteria.andEqualTo("d_date", searchMap.get("d_date"));
            }
            if (searchMap.get("d_time") != null) {
                criteria.andEqualTo("d_time", searchMap.get("d_time"));
            }
        }

        return example;


    }
}
